import { clearObject, deepCopyObj, copyObjBySelf } from '../../../assets/js/help.js'
import api from '../../../fetch/api'
export default {
  data() {
    var checkUserName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入账号'));
      } else {
        //var regexp = /^(?!_)(?!.*?_$)[a-zA-Z0-9_]+$/;
        //var regexp = /^[a-zA-Z0-9~!@#$%^&*()_<>]+$/;
        var regexp = /^[a-zA-Z0-9]+$/;
        var result = regexp.test(value);
        if (!result) {
          callback(new Error('账号仅支持英文字符、数字组合'));
        } else {
          var params = { userName: value, mobileUserId: this.userEditForm.id };
          api.fetchGet('/mobileUser/checkName.do', params).then(res => {
            if (res.status == 1) {
              if (res.data) {
                callback();
              } else {
                callback(new Error('此账号已被占用'));
              }
            } else {
              callback(new Error('验证错误，系统繁忙'));
            }
          }, err => { })
        }
      }
    };
    var checkRealName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入姓名'));
      } else {
        //var regexp = /^(?!_)(?!.*?_$)[a-zA-Z0-9_]+$/;
        var regexp = /^[a-zA-Z0-9~!@#$%^&*()_<>\u4e00-\u9fa5]+$/;
        var result = regexp.test(value);
        if (!result) {
          callback(new Error('姓名仅支持英文字符、数字、中文组合'));
        } else {
          callback();
        }
      }
    };
    var validatePass = (rule, value, callback) => {
      if (!this.userEditForm.id && value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.userEditForm.password) {
          var regexp = /^[a-zA-Z0-9~!@#$%^&*()_<>]+$/;
          var result = regexp.test(value);
          console.log(result);
          if (result) {
            callback();
          } else {
            callback(new Error('用户密码仅支持英文字符、数字、特殊符号~!@#$%^&*()_<>组合'));
          }
        }
      }
      callback();
    };
    var validatePass2 = (rule, value, callback) => {
      if (!this.userEditForm.id && value === '') {
        callback(new Error('请再次输入密码'));
      } else if (this.userEditForm.password && value !== this.userEditForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    var resetvalidatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.pwdForm.password !== '') {
          var regexp = /^[a-zA-Z0-9~!@#$%^&*()_<>]+$/;
          var result = regexp.test(value);
          console.log(result);
          if (result) {
            callback();
          } else {
            callback(new Error('用户密码仅支持英文字符、数字、特殊符号~!@#$%^&*()_<>组合'));
          }
        }
      }
    };
    var resetvalidatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.pwdForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };

    var telephone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入电话号码'));
      }
      // setTimeout(() => {
      if (!(/^1[34578]\d{9}$/.test(value))) {
        callback(new Error('请输入正确的电话号码'));
      } else {
        callback();
      }
      // }, 500);
    };

    return {
      editMobileUserId: "",
      userFormModalTitle: '',
      ParkListModalData: {
        list: [
          {
            id: '',
            mobileUserId: '',
            parkId: '',
            parkName: ''
          }
        ],
      },
      newUserEditForm: {
        id: null,
        userName: "",
        password: "",
        passwordAgain: "",
        realName: "",
        phone: "",
        email: "",
        address: "",
        desc: ''
      },
      userEditForm: {
        id: null,
        userName: "",
        password: "",
        passwordAgain: "",
        realName: "",
        phone: "",
        email: "",
        address: "",
        desc: ''
      },
      rules: {
        userName: [
          { required: true, message: '请输入账号', trigger: 'change' },
          { min: 2, max: 16, message: '长度在 2 到 16 个字符', trigger: 'blur' },
          { validator: checkUserName, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'change' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' },
          { validator: validatePass, trigger: 'blur' }
        ],
        realName: [
          { required: true, message: '请输入姓名', trigger: 'change' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' },
          { validator: checkRealName, trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入电话号码', trigger: 'change' },
          { validator: telephone, trigger: 'blur' }
        ],
        passwordAgain: [
          { required: true, message: '请再次输入密码', trigger: 'change' },
          { validator: validatePass2, trigger: 'blur' }
        ],
        address: [
          { min: 0, max: 100, message: '长度在 0 到 100 个字符', trigger: 'blur' },
        ],
        desc: [
          { min: 0, max: 100, message: '长度在 0 到 100 个字符', trigger: 'blur' },
        ],
        email:[
          { type: 'email', message: '电子邮箱格式不正确', trigger: 'blur,change' }
        ]
      }
    }
  },
  created() {
    //this.getList();
  },
  methods: {
    getList() {
      this.getItemList(this.searchParams);
    },
    getItemList(aparams) {//查询列表
      let _this = this,
        params = {
          page: this.pageNumber,
          rp: this.pageSize
        };
      params = $.extend({}, params, aparams);
      api.fetchGet('/mobileUser/list.do', params).then(res => {
        if (res.status == 1) {
          let data = res.data,
            list = data.rows;
          _this.tbodysInfo = list;
          _this.total = data.total;
        } else {
          self.showToast(res.message, 2, 2000)
          return;
        }
      }, err => { })
    },
    toOpenUserAdd(modalName) {//打开新增
      this.userFormModalTitle = "新增";

      var rule_password = this.rules.password;
      var rule_passwordAgain = this.rules.passwordAgain;
      if(rule_password.length==2){
        this.rules.password.unshift({ required: true, message: '请输入密码', trigger: 'change' });
      }
      if(rule_passwordAgain.length==1){
        this.rules.passwordAgain.unshift({ required: true, message: '请再次输入密码', trigger: 'change' });
      }

      this.userEditForm = deepCopyObj(this.newUserEditForm);
      clearObject(this.userEditForm);
      this.toOpenModal(modalName);
    },
    toOpenUserEdit(row, modalName) {//打开编辑
      this.userFormModalTitle = "编辑";

      var rule_password = this.rules.password;
      var rule_passwordAgain = this.rules.passwordAgain;
      if(rule_password.length==3){
        this.rules.password.shift();
      }
      if(rule_passwordAgain.length==2){
        this.rules.passwordAgain.shift();
      }

      this.userEditForm = deepCopyObj(row);
      this.toOpenModal(modalName);
    },
    submitForm(formName) {
      var self = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          self.saveForm();
        } else {
          return false;
        }
      });
    },//submitForm end
    saveForm() {//保存用户
      let self = this;
      var params = deepCopyObj(self.userEditForm);
      delete params.parkList;
      for (var i in params) {
        if (params[i] === null || params[i] === '') {
          delete params[i];
        }
      }
      delete params.createBy;
      delete params.createTime;
      delete params.updateBy;
      delete params.updateTime;
      api.fetchPost('/mobileUser/save.do', params).then(res => {
        if (res.status == 1) {
          self.showToast(res.message, 1, 2000);
          self.closed('userFormModal', 1);
          self.getList();
        } else {
          self.showToast(res.message, 2, 2000)
          return;
        }
      }, err => { })
    },
    toOpenUserDelete(mobileUser) {
      var self = this;
      var params = {
        mobileUserId: mobileUser.id,
      };
      self.showDialog("确认删除该记录吗?", '', function () {
        api.fetchPost('/mobileUser/del.do', params).then(res => {
          if (res.status == 1) {
            var data = res;
            self.showToast(res.message, 1);
            self.getList();
          } else {
            self.showToast(res.message, 2, 2000)
            return;
          }
        }, err => { })
      })
    },
    toOpenBindPark(row, modalName, treeId) {//打开设置权限对话框
      let self = this;
      self.editMobileUserId = row.id;
      self.toOpenModal(modalName, 1);
      self.initParkTree(treeId, row.parkList);
    },
    initParkTree(treeId, parkList) {//初始化权限树
      API.platform_manage.GetParkingGroupAreaParkTree({}).then(res => {
        $.fn.zTree.destroy(treeId);
        var settings = {
          check: {
            enable: true
          },
          data: {
            simpleData: {
              enable: true
            }
          },
          view: {
            showIcon: false,
            showLine: false,
          }
        };
        $.fn.zTree.init($("#" + treeId), settings, res.data.children);

        var treeObj = $.fn.zTree.getZTreeObj(treeId);
        treeObj.expandAll(true);
        for (var i = 0; i < parkList.length; i++) {
          var node = treeObj.getNodeByParam("value", parkList[i].parkId);
          if (node) {
            treeObj.checkNode(node, true, true);
          }
        }
      }, err => { });
    },
    bindParkingGroup(treeId) {//保存权限
      var self = this;
      var tree = $.fn.zTree.getZTreeObj(treeId);
      var nodes = tree.getCheckedNodes(true);
      var parkList = [];
      for (var i = 0; i < nodes.length; i++) {
        if (!nodes[i].isArea) {
          parkList.push(nodes[i].value);
        }
      }
      if (parkList.length <= 0) {
        self.showToast('请至少选择一个停车场', 0);
        return false;
      }
      var params = {
        'mobileUserId': self.editMobileUserId,
        'parkIdList': parkList.join(",")
      };
      api.fetchPost('/mobileUser/bindPark.do', params).then(res => {
        if (res.status == 1) {
          var data = res;
          self.showToast(res.message, 1);
          self.getList();
          self.closed('bindParkModal');
        } else {
          self.showToast(res.message, 2, 2000)
          return;
        }
      }, err => { })
    },
    ParkListModal_open(mobileUser){
      this.ParkListModalData.list = mobileUser.parkList;
      this.toOpenModal('ParkListModal', 1);
    }
  }
}